package route

import (
	Log "hello/Log"
	ctl "hello/controller"
	Mid "hello/middware"
	"net/http"
	"time"

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
)

func InitRoute() *gin.Engine {
	r := gin.New()
	r.Use(Mid.ZapLogger(Log.Logger), Mid.ZapRecovery(Log.Logger, true), Mid.RateLimitMiddleware(2*time.Second, 1))

	v1 := r.Group("api/v1")
	v1.POST("/signup", ctl.SignUpHandler)
	v1.POST("/login", ctl.LoginHandler)

	v1.GET("/ping", Mid.JWTAuthMiddleware(), func(c *gin.Context) {
		//如果是登录的用户，判断请求投中是否有 有效的jwt？
		c.String(http.StatusOK, "pong")
	})

	{
		v1.GET("/community", ctl.CommunityHandler)

		// v1.GET("/community", ctl.CommunityHandler)
	}

	r.NoRoute(func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"msg": "404",
		})
	})

	pprof.Register(r) //注册pprof相关路由

	r.Run("127.0.0.1:8090")
	return r
}
